<?php
namespace Admin\Controller;
use Think\Controller;

class UploadController extends Controller{

    public function __construct(){
        parent::__construct();
        $this->verification();
    }

    //添加栏目方法
    public function cateadd(){
        if(IS_POST){
            $cate=D('cate');
            if(!$cate->create()){
                $error=$cate->getError();
                echo "<script>alert('$error');location.href='javascript:history.go(-1)';</script>";
                exit;
            }
            $cate->time=date("Y-m-d H:i:s",$cate->time);
            if(!$cate->add()){
                echo "<script>alert('添加栏目失败!');location.href='javascript:history.go(-1)';</script>";
            }else{
                echo "<script>alert('添加栏目成功!');location.href=document.referrer;</script>";
            }
        }
        $this->filecate(1);
        $this->artcate(2);
        $this->albumlist();
        $this->catelist();
        $this->display();
    }

    //验证是否登陆
    protected function verification(){
        $userModel=D('user');
        $userinfo=$userModel->where(array('account'=>cookie('account')))->find();
        if(!che() || !$userinfo){
            $url=U('User/login');
            echo "<script>alert('对不起，请您先登陆。');location.href='$url';</script>";
        }
    }

    //获取文件栏目
    protected function catelist(){
        $cateModel=D('cate');
        //$catelist=S('cate');
        $catelist=array();
        if(!$catelist){
            $catelist=$cateModel->gettree();
            //S('cate',$catelist,3);
        }
        //S('cate',null);    //清空缓存
        $this->assign('catelist',$catelist);
    }

    //获取文件栏目
    protected function filecate($pid){
        $cateModel=D('cate');
        $filecate=$cateModel->gettree($pid);
        $this->assign('filecate',$filecate);
    }

    //获取文章栏目
    protected function artcate($pid){
        $cateModel=D('cate');
        $artcate=$cateModel->gettree($pid);
        $this->assign('artcate',$artcate);
    }

    //添加相册方法
    public function album(){
        if(IS_POST){
            $album=D('album');
            if(!$album->create()){
                $error=$album->getError();
                echo "<script>alert('$error');location.href='javascript:history.go(-1)';</script>";
                exit;
            }
            $album->uid=$_COOKIE['uid'];
            $album->cr_time=date("Y-m-d H:i:s",$album->cr_time);
            $album->up_time=date("Y-m-d H:i:s",$album->up_time);

            $upload = new \Think\Upload();  //实例化上传类
            $upload->maxSize    =   314572800000000;    //设置附件上传大小
            $upload->exts       =   array('gif','png','jpeg','jpg');
            $upload->rootPath   = './Public/Uploads/'; // 设置附件上传根目录
            $upload->savePath   = 'Album/'; // 设置附件上传（子）目录
            // 上传文件
            $info = $upload->upload();
            if(!$info){
                $error=$upload->getError();
                echo "<script>alert('$error');location.href='javascript:history.go(-1)';</script>";
                exit;
            }else{
                $img_path1='./Public/Uploads/' . $info['album']['savepath'];
                $img_path2=$info['album']['savename'];
                $cover_img=$img_path1 . $img_path2;
                $thumb_img='./Public/Uploads/Album/Thumb/' . $img_path2;
                $image=new \Think\Image();
                $image->open($cover_img);
                $image->thumb(240,160,\Think\Image::IMAGE_THUMB_FILLED)->save($thumb_img);

                $album->cover_img=substr($cover_img,9);
                $album->thumb_img=substr($thumb_img,9);
            }
            if(!$album->add()){
                echo "<script>alert('添加相册失败!');location.href='javascript:history.go(-1)';</script>";
            }else{
                echo "<script>alert('添加相册成功!');location.href=document.referrer;</script>";
            }
        }
        $this->filecate(1);
        $this->artcate(2);
        $this->albumlist();
        $this->display();
    }

    //上传照片方法
    public function picture(){
        if(IS_POST){
            if(I('post.aid')==0){
                echo "<script>alert('请选择相册!');location.href='javascript:history.go(-1)';</script>";
                exit;
            }
            $picture=D('picture');
            if(!$picture->create()){
                $error=$picture->getError();
                echo "<script>alert('$error');location.href='javascript:history.go(-1)';</script>";
                exit;
            }
            $upload = new \Think\Upload();  //实例化上传类
            $upload->maxSize    =   314572800000000;    //设置附件上传大小
            $upload->exts       =   array('gif','png','jpeg','jpg','psd');
            $upload->rootPath = './Public/Uploads/'; // 设置附件上传根目录
            $upload->savePath = 'Picture/'; // 设置附件上传（子）目录
            // 上传文件
            $info = $upload->upload();
            if(!$info) {// 上传错误提示错误信息
                $error=$upload->getError();
                echo "<script>alert('$error');location.href='javascript:history.go(-1)';</script>";
                exit;
            }else{// 上传成功
                foreach($info as $k=>$v) {
                    $picture->create($_POST);
                    $picture->uid=$_COOKIE['uid'];
                    $picture->time=date("Y-m-d H:i:s",$picture->time);
                    $img_path1 = './Public/Uploads/' . $v['savepath'];
                    $img_path2 = $v['savename'];
                    $ori_img = $img_path1 . $img_path2;
                    //$water_file = './Public/Uploads/logo.png';
                    $image = new \Think\Image();
                    $image->open($ori_img);
                    //按照原图的比例生成一个最大为150*150的缩略图并保存为thumb.jpe
                    $thumb_img = './Public/Uploads/Picture/Thumb/' . $img_path2;
                    //$water_img = './Public/Uploads/water/' . $img_path2;

                    $image->thumb(120,80, \Think\Image::IMAGE_THUMB_FILLED)->save($thumb_img);
                    //$image->open($ori_img)->water($water_file, \Think\Image::IMAGE_WATER_SOUTHEAST, 30)->save($water_img);

                    //将图片路径储存进数据库 str_replace('待替换', '替换成', $str);
                    $picture->ori_img = substr($ori_img, 9);
                    $picture->thumb_img = substr($thumb_img, 9);
                    //$this->datas->water_img = substr($water_img, 9);
                    $picture->name=$v['savename'];
                    //有BUG!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
                    if(!$resule=$picture->add()){
                        echo "<script>alert('上传失败!');location.href='javascript:history.go(-1)';</script>";
                    }
                }
                //如果都上传成功则返回成功
                if($resule){
                    echo "<script>alert('上传成功!');location.href=document.referrer;</script>";
                    //window.location.href = document.referrer;            //返回上一页并刷新
                    //window.location.href='javascript:history.go(-1)';     //返回上一页不刷新
                    //document.parentWindow.location.reload();
                }
            }
        }
        $this->filecate(1);
        $this->artcate(2);
        $this->albumlist();
        $this->display();
    }

    //获取相册栏目与搜索栏的相册栏目
    protected function albumlist(){
        $albumModel=D('album');
        $albumlist=$albumModel->select();
        $this->assign('albumlist',$albumlist);
    }

    //上传文件方法
    public function file(){
        if(IS_POST){
            if(I('post.cid')==0){
                echo "<script>alert('请选择文件栏目!');location.href='javascript:history.go(-1)';</script>";
                exit;
            }
            $file=D('file');
            if(!$file->create()){
                $error=$file->getError();
                echo "<script>alert('$error');location.href='javascript:history.go(-1)';</script>";
                exit;
            }
            $upload = new \Think\Upload();  //实例化上传类
            $upload->maxSize    =   314572800000000;    //设置附件上传大小
            $upload->exts       =   array('gif','png','jpeg','jpg','psd','txt','doc','pdf','xls','xlsx','word','mp3','pptx','docx','doc','rar','zip','psd');
            $upload->rootPath = './Public/Uploads/'; // 设置附件上传根目录
            $upload->savePath = 'File/'; // 设置附件上传（子）目录
            // 上传文件
            $info = $upload->upload();
            if(!$info){
                $error=$upload->getError();
                echo "<script>alert('$error');location.href='javascript:history.go(-1)';</script>";
                exit;
            }else{
                $img_path1='./Public/Uploads/' . $info['file']['savepath'];
                $img_path2=$info['file']['savename'];
                $file_path=$img_path1 . $img_path2;
                $file->uid=$_COOKIE['uid'];
                $file->time=date("Y-m-d H:i:s",$file->time);
                $file->ori_name=$info['file']['name'];
                $file->name=$info['file']['savename'];
                $file->file=substr($file_path,9);
            }
            if(!$file->add()){
                echo "<script>alert('文件上传失败!');location.href='javascript:history.go(-1)';</script>";
            }else{
                echo "<script>alert('文件上传成功!');location.href=document.referrer;</script>";
            }
        }
        $this->filecate(1);
        $this->artcate(2);
        $this->albumlist();
        $this->display();
    }

    //上传文章方法
    public function article(){
        if(IS_POST){
            if(I('post.cid')==0){
                echo "<script>alert('请选择文章栏目!');location.href='javascript:history.go(-1)';</script>";
                exit;
            }
            $article=D('article');
            if(!$article->create()){
                $error=$article->getError();
                echo "<script>alert('$error');location.href='javascript:history.go(-1)';</script>";
                exit;
            }
            $article->uid=$_COOKIE['uid'];
            $article->time=date("Y-m-d H:i:s",$article->time);
            if(!$article->add()){
                echo "<script>alert('上传文章失败!');location.href='javascript:history.go(-1)';</script>";
            }else{
                echo "<script>alert('上传文章成功!');location.href=document.referrer;</script>";
            }
        }
        $this->filecate(1);
        $this->artcate(2);
        $this->albumlist();
        $this->display();
    }

}